/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.planning;

import gestionedt.controllers.EnseignantJpaController;
import gestionedt.controllers.GroupeJpaController;
import gestionedt.models.Enseignant;
import gestionedt.models.Groupe;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;

/**
 * 
 */
public class SelectGrpEnsAction extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward selectionnerPlusieurs(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        SelectionForm sForm = (SelectionForm) form;
        String recherche = sForm.getRecherche().trim();
        if( ! recherche.equals("")) { // si le critère de recherce est renseigné :
            // on recherche d'abord les groupes qui répondent à ce critère
            GroupeJpaController ctGrp = new GroupeJpaController();
            List<Groupe> groupes = ctGrp.findGroupesLike("%"+recherche+"%");
            String[] idsGroupes = new String[groupes.size()];
            int i = 0;
            for(Groupe groupe : groupes)
                idsGroupes[i++] = groupe.getId().toString();
            sForm.setIdsGrpSelectionnes(idsGroupes);
            // puis on recherche les enseignants qui répondent à ce critère
            EnseignantJpaController ctEns = new EnseignantJpaController();
            List<Enseignant> enseignants = ctEns.findEnseignantsLike("%"+recherche+"%");
            String[] idsEns = new String[enseignants.size()];
            i = 0;
            for(Enseignant enseignant : enseignants)
                idsEns[i++] = enseignant.getId().toString();
            sForm.setIdsEnsSelectionnes(idsEns);
        }
        
        return mapping.findForward(SUCCESS);
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward selectionnerUnGrp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        SelectionForm selForm = (SelectionForm) form;
        String idGroupe = selForm.getIdGrpSel();
        selForm.setIdsGrpSelectionnes(new String[] {idGroupe});
        selForm.setIdsEnsSelectionnes(new String[] {});
        selForm.setRecherche("");

        return mapping.findForward(SUCCESS);
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward selectionnerUnEns(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        SelectionForm selForm = (SelectionForm) form;
        String idEnseignant = selForm.getIdEnsSel();
        selForm.setIdsEnsSelectionnes(new String[] {idEnseignant});
        selForm.setIdsGrpSelectionnes(new String[] {});
        selForm.setRecherche("");

        return mapping.findForward(SUCCESS);
    }

}
